package com.music.listen.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.music.domain.Music;
import com.music.listen.dao.ListenDao;

@Repository
public class ListenDaoImpl implements ListenDao {
	@Autowired
	SessionFactory sessionFactory;
	@Override
	public List<Music> loadListenMusic(String userid) {
		String sql="select distinct(cateid) from t_music where id in(select distinct(musicid) from t_user_music where userid=:userid)";
		String hql="from Music where cateid in (:cates) order by createdate desc";
		Session session = sessionFactory.getCurrentSession();
		SQLQuery sqlQuery = session.createSQLQuery(sql);
		sqlQuery.setParameter("userid", userid);
		List<String> cates = sqlQuery.list();
		Query hqlQuery = session.createQuery(hql);
		/**
		 * 判断cates是否为空
		 */
		if(cates==null||cates.size()<=0){
			cates.add("1");
		}
		hqlQuery.setParameterList("cates", cates);
		hqlQuery.setFirstResult(0);
		hqlQuery.setMaxResults(20);
		List<Music> data = hqlQuery.list();
		return data;
	}

}
